//
//  VideoDetailController.m
//  FoodList
//
//  Created by Jason on 16/4/8.
//  Copyright © 2016年 王金晶. All rights reserved.
//

#import "VideoDetailController.h"
#import "DetailView.h"
#import "VideoModel.h"
#import "MoviePlayController.h"
#import "AppDelegate.h"
#import "PlayerManager.h"

//typedef NS_ENUM(NSInteger, IPhoneStyle) {
//    IPhoneStyle4s = 480,
//    IPhoneStyle5s = 568,
//    IPhoneStyle6s = 667,
//    IPhoneStyle6P = 736
//};

@interface VideoDetailController () <UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *bigScrollView;
@property (nonatomic, strong) DetailView *detailView;

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) CGPoint oldContentOfSet;


@end

@implementation VideoDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.tabBarController.tabBar.hidden = YES;
    
    if (_number == 0) {
        _oldContentOfSet = CGPointMake(0, 0);
    }else {
        _oldContentOfSet = CGPointMake(Width * _number, 0);
    }
    
    [self creatView];
    
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(backButton:)];
    self.navigationItem.leftBarButtonItem = leftBarButton;
    
//    self.timer = [NSTimer scheduledTimerWithTimeInterval:8 target:self selector:@selector(changeZoom) userInfo:nil repeats:YES];
//    [self performSelector:@selector(changeZoom) withObject:nil afterDelay:0];
    
    
}

- (void)backButton:(UIBarButtonItem *)barButton {
    [self.navigationController popViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden = NO;
}
#pragma mark - scrollView 滑动方法 -
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    UIImageView *tempImg1 = [self.view viewWithTag:500];
    UIImageView *tempImg2 = [self.view viewWithTag:600];
    
    if (scrollView.contentOffset.x < _oldContentOfSet.x) {
        CGFloat alpha = ((NSInteger)scrollView.contentOffset.x  %  (NSInteger)Width) / Width;
        _detailView.alpha = alpha;
        tempImg1.alpha = alpha;
        tempImg2.alpha = 0;
    }else {
        CGFloat alpha = (scrollView.contentOffset.x - _oldContentOfSet.x) / Width;
        _detailView.alpha = 1 - alpha;
        tempImg1.alpha = 1 - alpha;

    }
//    NSLog(@"_detailView.alpha === %.2f", _detailView.alpha);
}

#pragma mark - scrollView 结束减速代理方法 -
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    // 记录老的整屏偏移量
    _oldContentOfSet = scrollView.contentOffset;
    NSInteger index = scrollView.contentOffset.x / Width;
    _detailView.model = _VideoModelArray[index];
    UIImageView *tempImg1 = [self.view viewWithTag:500];
    UIImageView *tempImg2 = [self.view viewWithTag:600];
    UIImageView *tempImg3 = [self.view viewWithTag:700];
    tempImg1.alpha = 1;
    tempImg2.alpha = 1;
    [tempImg1 sd_setImageWithURL:[NSURL URLWithString:[_VideoModelArray[index] coverBlurred]]];
    if (index + 1 > _VideoModelArray.count) {
        [tempImg2 sd_setImageWithURL:[NSURL URLWithString:[_VideoModelArray.lastObject coverBlurred]]];
    }else {
        [tempImg2 sd_setImageWithURL:[NSURL URLWithString:[_VideoModelArray[index + 1] coverBlurred]]];
    }
    if (index - 1 < 0) {
        [tempImg3 sd_setImageWithURL:[NSURL URLWithString:[_VideoModelArray.firstObject coverBlurred]]];
    }else {
        [tempImg3 sd_setImageWithURL:[NSURL URLWithString:[_VideoModelArray[index - 1] coverBlurred]]];
    }
    [UIView animateWithDuration:1 animations:^{
        _detailView.alpha = 1;
    }];
}
#pragma mark - 创建视图 -
- (void)creatView {
    self.bigScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, Width, Height)];
    self.bigScrollView.backgroundColor = [UIColor clearColor];
    _bigScrollView.delegate = self;
    _bigScrollView.pagingEnabled = YES;
    _bigScrollView.showsVerticalScrollIndicator = NO;
    _bigScrollView.showsHorizontalScrollIndicator = NO;
    _bigScrollView.bounces = NO;
    _bigScrollView.contentSize = CGSizeMake(Width * _VideoModelArray.count, 0);
    
    for (int i = 0; i < _VideoModelArray.count; i++) {
        UIScrollView *smallScr = [[UIScrollView alloc] initWithFrame:CGRectMake(Width * i, 0, Width, Height / 2 - 20)];
        smallScr.delegate = self;
        smallScr.contentSize = CGSizeMake(Width, 380);
        smallScr.scrollEnabled = NO;
        smallScr.minimumZoomScale = 1;
        smallScr.maximumZoomScale = 1.1;
        UIImageView *upImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, Width, smallScr.frame.size.height)];
        upImg.tag = 1000 + i;
        upImg.userInteractionEnabled = YES;
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageNamed:@"play.png"] forState:UIControlStateNormal];
        button.frame = CGRectMake((Width - 50) / 2, (upImg.frame.size.height - 50) / 2, 50, 50);
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
  //      button.backgroundColor = [UIColor redColor];
        [upImg addSubview:button];
        VideoModel *model = _VideoModelArray[i];
        [upImg sd_setImageWithURL:[NSURL URLWithString:model.coverForSharing]];
        [smallScr addSubview:upImg];
        [_bigScrollView addSubview:smallScr];
        
    }
    
    UIImageView *downImg1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, Height / 2 - 20, Width, Height / 2 - 44)];
    downImg1.userInteractionEnabled = YES;
    downImg1.tag = 500;
    UIImageView *downImg2 = [[UIImageView alloc] initWithFrame:downImg1.frame];
    downImg2.tag = 600;
    downImg2.userInteractionEnabled = YES;
    UIImageView *downImg3 = [[UIImageView alloc] initWithFrame:downImg1.frame];
    downImg3.userInteractionEnabled = YES;
    downImg3.tag = 700;
    
    VideoModel *model = _VideoModelArray[_number];
    [downImg1 sd_setImageWithURL:[NSURL URLWithString:model.coverBlurred]];
    if (_number + 1 > _VideoModelArray.count) {
        [downImg1 sd_setImageWithURL:[NSURL URLWithString:[_VideoModelArray.lastObject coverBlurred]]];
    }else {
        [downImg2 sd_setImageWithURL:[NSURL URLWithString:[_VideoModelArray[_number + 1] coverBlurred]]];
    }
    if (_number - 1 < 0) {
        [downImg3 sd_setImageWithURL:[NSURL URLWithString:[_VideoModelArray.firstObject coverBlurred]]];
    }else {
        [downImg3 sd_setImageWithURL:[NSURL URLWithString:[_VideoModelArray[_number - 1] coverBlurred]]];
    }
    [self.view addSubview:downImg3];
    [self.view addSubview:downImg2];
    [self.view addSubview:downImg1];
    
    // 蒙板
    UIView *mengBan = [[UIView alloc] initWithFrame:downImg1.frame];
    mengBan.backgroundColor = [UIColor blackColor];
    mengBan.alpha = 0.3;
    
    self.detailView = [[NSBundle mainBundle] loadNibNamed:@"DetailView" owner:nil options:nil].firstObject;
    _detailView.backgroundColor = [UIColor clearColor];
    _detailView.alpha = 1;
    _detailView.frame = CGRectMake(0, 0, downImg1.frame.size.width, downImg1.frame.size.height);
    _detailView.titleLab.font = [UIFont systemFontOfSize:returnFont(Height) + 2];
    _detailView.durationLab.font = [UIFont systemFontOfSize:returnFont(Height)];
    _detailView.detailLab.font = [UIFont systemFontOfSize:returnFont(Height)];
    _detailView.model = model;
    [_detailView.collectBtn addTarget:self action:@selector(collectBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [_detailView.shareBtn addTarget:self action:@selector(shareBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [mengBan addSubview:_detailView];
    [self.view addSubview:mengBan];
    _bigScrollView.frame = CGRectMake(0, 0, Width, Height - 104);
    [self.view addSubview:_bigScrollView];
    
    // 改变上部 scrollView 偏移量
    _bigScrollView.contentOffset = CGPointMake(Width * _number, 0);
}

- (void)collectBtnAction:(UIButton *)button {
    NSLog(@"收藏");
}
- (void)shareBtnAction:(UIButton *)button {
    NSLog(@"分享");
}
 
- (void)buttonAction:(UIButton *)button {
    [[PlayerManager shardedManager] pause];
    MoviePlayController *movieVc = [[MoviePlayController alloc] init];
    movieVc.model = _detailView.model;
    AppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
    appDelegate.isFlip = YES;
    [self presentViewController:movieVc animated:YES completion:nil];
}

/*
- (void)creatViewOther {
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, Width, 380)];
    _scrollView.contentSize = CGSizeMake(Width, 0);
    _scrollView.contentOffset = CGPointMake(0, 0);
    _scrollView.delegate = self;
    _scrollView.minimumZoomScale = 1;
    _scrollView.maximumZoomScale = 1.2;
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, Width, 380)];
    [image sd_setImageWithURL:[NSURL URLWithString:[_VideoModelArray[_number] coverForSharing]]];
    [_scrollView addSubview:image];
    [self.view addSubview:_scrollView];
    
    UIImageView *downImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 380, Width, Height - 444)];
    VideoModel *model = _VideoModelArray[_number];
    [downImg sd_setImageWithURL:[NSURL URLWithString:model.coverBlurred]];
    [self.view addSubview:downImg];
    
    DetailView *detailView = [[NSBundle mainBundle] loadNibNamed:@"DetailView" owner:nil options:nil].firstObject;
    detailView.backgroundColor = [UIColor clearColor];
    detailView.alpha = 1;
    detailView.frame = CGRectMake(0, 0, Width, downImg.frame.size.height);
    detailView.model = model;
    [downImg addSubview:detailView];
 
}

- (void)changeZoom {
    [UIView animateWithDuration:4 animations:^{
        _scrollView.zoomScale = 1.08;
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:4 animations:^{
            _scrollView.zoomScale = 1;
        }];
    }];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    
    NSInteger t = _bigScrollView.contentOffset.x / Width;
    NSLog(@"%@", [_bigScrollView.subviews[t].subviews[0] class]);
    return _bigScrollView.subviews[t].subviews[0];
}
 */

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
